package library.and.xps.com.step.view.activity;

import android.content.Context;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.squareup.okhttp.Request;
import com.zhy.autolayout.AutoRelativeLayout;

import library.and.xps.com.step.R;
import library.and.xps.com.step.URLConstants;
import library.and.xps.com.step.m.model.RequestParams;
import library.and.xps.com.step.m.netapi.OkHttpClientUtils;
import library.and.xps.com.step.net.ResponceWrapper;
import library.and.xps.com.step.util.HttpResponsHelper;
import library.and.xps.com.step.util.RequestCallback;
import library.and.xps.com.step.util.TeamHelper;
import library.and.xps.com.step.view.base.BaseActivity;

public class TeamDescriptionAvtivity extends BaseActivity {
    AutoRelativeLayout alaction;
    TextView actionbar_tv_right;
    EditText et_pass;

    @Override
    protected int getLayout() {
        return R.layout.activity_team_desc;
    }

    @Override
    protected void setData() {

    }

    @Override
    protected void init() {
        ((TextView)r2v(R.id.actionbar_tv_title)).setText("团队描述");
        r2v(R.id.actionbar_iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        alaction = r2v(R.id.actionbar_al);
        et_pass = r2v(R.id.et_pass);
        actionbar_tv_right = r2v(R.id.actionbar_tv_right);
        actionbar_tv_right.setText("确定");
        actionbar_tv_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toogleSoftInput();
                commitNewDescription(et_pass.getText().toString());
            }
        });
    }

    void commitNewDescription(String Description){
        String json = RequestParams.getNewInstance().setUserid(getUser().getUserid()).setTeamid(TeamHelper.getIns().getTid()).setDescription(Description).getJson().toString();
        OkHttpClientUtils.postJsonStringAsync(this, URLConstants.TEAM.UpdateTeamInformation, json, new RequestCallback() {
            @Override
            public void onFailure(Request request, Exception e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(ResponceWrapper response) {
                if(HttpResponsHelper.SuccessResponce(response)){
                    Snackbar.make(alaction, "团队简介已修改", Snackbar.LENGTH_LONG)
                            .setAction("确定", null).show();
                }
            }

            @Override
            public void onStart() {

            }
        },"");
    }

    void toogleSoftInput(){
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }

}
